Ensure TARGET is always present for build commands
authorAlex Crichton <alex@alexcrichton.com>
Sat, 16 Aug 2014 19:27:31 +0000 (12:27 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sat, 16 Aug 2014 19:41:17 +0000 (12:41 -0700)
src/cargo/ops/cargo_rustc/context.rs
src/cargo/ops/cargo_rustc/mod.rs
tests/test_cargo_bench.rs

index 87934fa64960d03d2dbc0584ea62cd49e2ca21bd..96186d5351cf81a169c3b92fb148792c4e480882 100644 (file)
@@ -4,7 +4,7 @@ use semver::Version;
 
 use core::{SourceMap, Package, PackageId, PackageSet, Resolve, Target};
 use util;
-use util::{CargoResult, ChainError, internal, Config, profile};
+use util::{CargoResult, ChainError, internal, Config, profile, Require};
 
 use super::{Kind, KindPlugin, KindTarget, Compilation};
 use super::layout::{Layout, LayoutProxy};
@@ -27,6 +27,7 @@ pub struct Context<'a, 'b> {
     env: &'a str,
     host: Layout,
     target: Option<Layout>,
+    target_triple: String,
     host_dylib: (String, String),
     package_set: &'a PackageSet,
     target_dylib: (String, String),
@@ -47,8 +48,10 @@ impl<'a, 'b> Context<'a, 'b> {
             let (dylib, _) = try!(Context::filename_parts(None));
             dylib
         };
+        let (rustc_version, target_triple) = try!(Context::rustc_version());
         Ok(Context {
-            rustc_version: try!(Context::rustc_version()),
+            rustc_version: rustc_version,
+            target_triple: target_triple,
             env: env,
             host: host,
             target: target,
@@ -65,11 +68,26 @@ impl<'a, 'b> Context<'a, 'b> {
         })
     }
 
-    /// Run `rustc` to figure out what its current version string is
-    fn rustc_version() -> CargoResult<String> {
+    /// Run `rustc` to figure out what its current version string is.
+    ///
+    /// The second element of the tuple returned is the target triple that rustc
+    /// is a host for.
+    fn rustc_version() -> CargoResult<(String, String)> {
         let output = try!(util::process("rustc").arg("-v").arg("verbose")
                                .exec_with_output());
-        Ok(String::from_utf8(output.output).unwrap())
+        let output = try!(String::from_utf8(output.output).map_err(|_| {
+            internal("rustc -v didn't return utf8 output")
+        }));
+        let triple = {
+            let triple = output.as_slice().lines().filter(|l| {
+                l.starts_with("host: ")
+            }).map(|l| l.slice_from(6)).next();
+            let triple = try!(triple.require(|| {
+                internal("rustc -v didn't have a line for `host:`")
+            }));
+            triple.to_string()
+        };
+        Ok((output, triple))
     }
 
     /// Run `rustc` to discover the dylib prefix/suffix for the target
@@ -205,6 +223,11 @@ impl<'a, 'b> Context<'a, 'b> {
         (pair.ref0().as_slice(), pair.ref1().as_slice())
     }
 
+    /// Return the target triple which this context is targeting.
+    pub fn target_triple(&self) -> &str {
+        self.target_triple.as_slice()
+    }
+
     /// Return the exact filename of the target.
     pub fn target_filenames(&self, target: &Target) -> Vec<String> {
         let stem = target.file_stem();
index 40d443e3c9f9acd9d16c25be1c08fcb25b698894..ca0f6e843f1cbc81e9c35e2217aa1f2bf9303320 100644 (file)
@@ -167,7 +167,7 @@ fn compile_custom(pkg: &Package, cmd: &str,
     let mut p = process(cmd.next().unwrap(), pkg, cx)
                      .env("OUT_DIR", Some(&output))
                      .env("DEPS_DIR", Some(&output))
-                     .env("TARGET", cx.config.target());
+                     .env("TARGET", Some(cx.target_triple()));
     for arg in cmd {
         p = p.arg(arg);
     }
index 9c2e85ca16db0b89c76321911a65bcf58f13f8bd..cae06099c41560d29f1808955fea43ecbf7b354a 100644 (file)
@@ -482,7 +482,7 @@ test!(lib_bin_same_name {
             version = "0.0.1"
             authors = []
 
-            [[lib]]
+            [lib]
             name = "foo"
             [[bin]]
             name = "foo"
@@ -591,7 +591,7 @@ test!(lib_with_standard_name2 {
             version = "0.0.1"
             authors = []
 
-            [[lib]]
+            [lib]
             name = "syntax"
             bench = false
             doctest = false
@@ -666,7 +666,7 @@ test!(bench_dylib {
             version = "0.0.1"
             authors = []
 
-            [[lib]]
+            [lib]
             name = "foo"
             crate_type = ["dylib"]
 
@@ -695,7 +695,7 @@ test!(bench_dylib {
             version = "0.0.1"
             authors = []
 
-            [[lib]]
+            [lib]
             name = "bar"
             crate_type = ["dylib"]
         "#)